package org.mat.ipaas.ucenter.service.util;

import lombok.extern.slf4j.Slf4j;
import org.mat.framework.core.context.RequestContextConstants;
import org.mat.framework.core.context.RequestContextUtils;

@Slf4j
public class SessionUtils {

    private static String accessTokenCtxKey = RequestContextConstants.PREFIX + "access-token";

    private static String userNameCtxKey = RequestContextConstants.PREFIX + "user-name";

    private static String userIdCtxKey = RequestContextConstants.PREFIX + "user-id";


    /**
     * TODO 从 RequestContext or 入参 获取登录用户？
     *
     * @return
     */
    public static void setAccessToken(String accessToken) {
        log.info("==== 保存用户信息到RequestContext，accessToken = {}", accessToken);
        RequestContextUtils.put(accessTokenCtxKey, accessToken);
        log.info("==== data = {}", RequestContextUtils.get().getData());
    }


    /**
     * TODO 从 RequestContext or 入参 获取登录用户？
     *
     * @return
     */
    public static void setLoginUserName(String userName) {
        log.info("==== 保存用户信息到RequestContext，userName = {}", userName);
        RequestContextUtils.put(userNameCtxKey, userName);
        log.info("==== data = {}", RequestContextUtils.get().getData());
    }

    /**
     * TODO 从 RequestContext or 入参 获取登录用户？
     *
     * @return
     */
    public static void setLoginUserId(String userId) {
        log.info("==== 保存用户信息到RequestContext，userId = {}", userId);
        RequestContextUtils.put(userIdCtxKey, userId);
        log.info("==== data = {}", RequestContextUtils.get().getData());
    }

    /**
     * TODO 从 RequestContext or 入参 获取登录用户？
     *
     * @return
     */
    public static String getAccessToken() {
        String accessToken = RequestContextUtils.get(accessTokenCtxKey);
        log.info("==== 从RequestContext获取用户信息，accessToken = {}", accessToken);
        return accessToken;
    }

    /**
     * TODO 从 RequestContext or 入参 获取登录用户？
     *
     * @return
     */
    public static String getLoginUserName() {
        String userName = RequestContextUtils.get(userNameCtxKey);
        log.info("==== 从RequestContext获取用户信息，userName = {}", userName);
        return userName;
    }

    /**
     * TODO 从 RequestContext or 入参 获取登录用户？
     *
     * @return
     */
    public static String getLoginUserId() {
        String userId = RequestContextUtils.get(userIdCtxKey);
        log.info("==== 从RequestContext获取用户信息，userId = {}", userId);
        return userId;
    }

}
